package com.hejian.android.a72ggametools.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.bean.PrizeDetail;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;
import com.hejian.android.a72ggametools.net.HttpUtils;
import com.hejian.android.a72ggametools.utils.JsonParse;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/22.
 */
public class PrizeDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView prizeImg;
    private TextView prizeContent;
    private TextView prizeRemain;
    private Button prizeConvert;
    private ImageCache imageCache = new DoubleCache();
    private ImageLoader imageLoader;
    private String id;
    private PrizeDetail prizeDetail;
    private Button prizeBackBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prize_detail);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        prizeBackBtn.setOnClickListener(this);
        prizeConvert.setOnClickListener(this);


    }

    private void initData() {
        imageLoader = ImageLoader.getInstance(getApplicationContext(),imageCache);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        loadData();
    }

    private void loadData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadPrizeDetail();
            }
        }).start();
    }

    private void loadPrizeDetail() {
        Map<String,String> map = new HashMap<String, String>();
        map.put("id",id);
        String buffer = HttpUtils.doPostRequest(Constant.PRIZE_DETAIL_URL,map);
        PrizeDetail detail = JsonParse.parsePrizeDetailJson(buffer);
        Message msg = Message.obtain();
        msg.what = 1;
        msg.obj = detail;
        handler.sendMessage(msg);

    }

    private void initView() {
        prizeBackBtn = (Button)findViewById(R.id.prize_back);
        prizeImg = (ImageView)findViewById(R.id.prize_img);
        prizeContent = (TextView)findViewById(R.id.prize_content);
        prizeRemain = (TextView)findViewById(R.id.prize_remain);
        prizeConvert = (Button)findViewById(R.id.prize_convert);
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1){
                prizeDetail = (PrizeDetail) msg.obj;
                prizeContent.setText(prizeDetail.getName());
                prizeRemain.setText(prizeDetail.getRemain());
                prizeConvert.setText("立即兑换（"+prizeDetail.getConsume()+"U币)");
                imageLoader.displayImage(prizeDetail.getIcon(),prizeImg);
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.prize_back:
                finish();
                break;
            case R.id.prize_convert:
                Intent intent = new Intent(PrizeDetailActivity.this,LoginActivity.class);
                startActivity(intent);
        }
    }
}
